<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class LunboController extends Controller
{
    public function getAdd()
    {
        return view('admin.lunbo.add');
    }

    // 上传
    public function postInsert(Request $request)
    {
        $res = $request->except('_token');
        // dd($res);
        // 处理图像
        $d = [];
        $tmp = [];
        // 获取所有的文件对象
        $files=$request->file('upload');
        // dd($files);
        foreach ($files as $k => $v) {
            $fileName = time().rand(100000,999999);
            $suffix = $v->getClientOriginalExtension();
            $fileName = $fileName.'.'.$suffix;
            $dir = './Uploads/lunbo/';
            $v->move($dir,$fileName);
            // 拼接OK的图片路径(绝对路径)
            $tmp['pic'] = trim($dir.$fileName,'.');
            $d[$k]['pic'] = $tmp['pic'];
            $d[$k]['address'] =  $res['address'];
            $d[$k]['status'] =  $res['status'];
        }
        // dd($d);
        // 插入数据库
        $id = DB::table('shop_carouselpics')->insert($d);
        if($id){
            return redirect('/admin/lunbo/index')->with('info','上传成功');
        }else{
            return back()->with('info','上传失败');
        }
    }

    // 列表显示
    public function getIndex(Request $request)
    {
        $res = DB::table('shop_carouselpics')->get();
        // dd($res);
        return view('admin.lunbo.index',['res'=>$res,'request'=>$request]);
    }

    //图像的删除
    public function getDelete(Request $request)
    {
        $id = $request->input();
        $res = DB::table('shop_carouselpics')->where('id',$id)->delete();
        if($res){
            return back()->with('info','删除成功');
        }else{
            return back()->with('info','删除失败');
        }
    }

    // 图像的修改
    public function postEdit(Request $request)
    {
        // 获取id
        $id = $request->except('_token');
        // dd($id);
        // 修改id所对应的状态
        $status = DB::table('shop_carouselpics')->where('id',$request->input('id'))->update($id);
        if($status){
            return redirect('/admin/lunbo/index')->with('info','修改成功');
        }else{
            return back()->with('info','修改失败');
        }

        
    }
    
}
